<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Message
 *
 * @author QuangKhoi
 */
class Message {
    /**
     *
     * @var Int
     */
    public $messageId;
    /**
     *
     * @var String
     */
    public $threadTitle;
    /**
     * @var String
     */
    public $lead;
    /**
     * @var String
     */
    public $text;
    /**
     * @var String
     */
    public $source;
    /**
     * @var String
     */
    public $url;
    /**
     * @var String
     */
    public $author;
    /**
     * @var String
     */
    public $postTitle;
    /**
     * @var DateTime
     */
    public $time;
    /**
     * @var Int
     */
    public $authorId;
    /**
     * @var Int
     */
    public $threadId;
    /**
     * @var Int
     */
    public $SMT;
    
    public function __construct() {
        
    }
    
    /**
     * Create new Message object store all data
     * @param int $messageId
     * @param string $threadTitle
     * @param string $lead
     * @param string $text
     * @param string $source
     * @param string $url
     * @param string $author
     * @param string $postTitle
     * @param DateTime $time
     * @param Int $authorId
     * @param Int $threadId
     * @param Int $SMT
     */
    public static function withData($messageId, $threadTitle, $lead, $text, 
            $source, $url, $author, $postTitle, $time, $authorId, $threadId, $SMT) {
        /**
         * Assign data
         */
        $this->messageId = $messageId;
        $this->threadTitle = $threadTitle;
        $this->lead = $lead;
        $this->text = $text;
        $this->source = $source;
        $this->url = $url;
        $this->author = $author;
        $this->postTitle = $postTitle;
        $this->time = $time;
        $this->authorId = $authorId;
        $this->threadId = $threadId;
        $this->SMT = $SMT;
    }
    
    /**
     * Get message from server with specified id
     * @param int $messageId
     */
    public static function withId($messageId) {
        /*$instance = new self();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM storedmessages WHERE messageid = '$messageId'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->messageId = $rows["messageid"];
                $instance->threadTitle = $rows["threadtitle"];
                $instance->lead = $rows["lead"];
                $instance->text = $rows["text"];
                $instance->source = $rows["source"];
                $instance->url = $rows["url"];
                $instance->author = $rows["author"];
                $instance->time = date_create($rows["time"]);
                $instance->authorId = $rows["authorid"];
                $instance->threadId = $rows["threadid"];
                $instance->SMT = $rows["smt"];
                return $instance;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any stream with provided id");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }*/
        $post = new ZPostReader($messageId);
        return $post->read();
    }
    
    
    public function Save() {
        
    }
}

?>
